package iosr11.ui.validators;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class DateValidator implements Validator {

	private static final String errorDetail = "Bledna data.";
	private static final String	error = "Bledna data.";
	
	@Override
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		if (value == null) return;
		
		if(!dateCheck(value.toString())){
			FacesMessage msg = new FacesMessage();	//Messages.getMessage("iosr11.ui.common.messages", "badLogin", null);
			msg.setDetail(errorDetail);
			msg.setSummary(error);
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
		}

	}

	private boolean dateCheck(String txt) {
		try{
			Date date = DateFormat.getDateInstance().parse(txt);
			return true;
		}catch(ParseException ex){
		}
		return false;
	}

}
